<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Dyn extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $user_name = trim(input('user_name'), ' ');
            $h_type = trim(input('h_type'), ' ');
            $h_cate = trim(input('h_cate'), ' ');
            $where['h_status'] = ['neq', 3];
            if(isset($user_name) && $user_name != ''){
                $where['user_username|h_content'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($h_type) && $h_type != ''){
                $where['h_type'] = $h_type;
            }
            if(isset($h_cate) && $h_cate != ''){
                $where['h_cate'] = $h_cate;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_hot h')
                ->join('up_u_user u', 'h.h_user_id = u.user_id', 'left')
                ->join('up_hot_type ht', 'h.h_type = ht.type_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_hot h')
                ->join('up_u_user u', 'h.h_user_id = u.user_id', 'left')
                ->join('up_hot_type ht', 'h.h_type = ht.type_id', 'left')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('h_status asc,h_top desc,h_update_time desc')
                ->field('h.*,user_username,user_phone,type_name')
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    if($v['h_image'] && $v['h_cate'] == 1){
                        $lists[$k]['h_image'] = explode(',',$v['h_image']);
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $types = Db::table('up_hot_type')
            ->where('type_status',1)
            ->select();
        $this->assign('types',$types);
        return $this->fetch();
    }
    
    /**
     * 帖子详情
     * @return mixed
     */
    public function dynDetail(){
        $h_id = input('h_id');
        $detail = Db::table('up_hot h')
            ->join('up_u_user u', 'h.h_user_id = u.user_id', 'left')
            ->join('up_hot_type ht', 'h.h_type = ht.type_id', 'left')
            ->field('h.*, user_username, type_name')
            ->where('h_id', $h_id)
            ->find();
        if($detail['h_image'] && $detail['h_cate'] == 1){
            $img = trim($detail['h_image'], ',');
            $detail['h_image'] = explode(',', $img);
        }
        $this->assign('detail', $detail);
        return $this->fetch('dyn/dyndetail');
    }
    
    /**
     * 删除帖子
     * @return \think\response\Json
     */
    public function dynDel(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_status',3);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 下架
     * @return \think\response\Json
     */
    public function xiajia(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 上架
     * @return \think\response\Json
     */
    public function shangjia(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_status',1);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 推荐
     * @return \think\response\Json
     */
    public function topyes(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_top',2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 取消推荐
     * @return \think\response\Json
     */
    public function topno(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_top',1);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    
    /**
     * 评论
     * @return mixed|\think\response\Json
     */
    public function dynComment(){
        if(request()->isAjax()){
            $where['comment_hot_id'] = session('h_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_comment')
                ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_comment c')
                ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
                ->field('c.*, user_username')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('comment_god desc,comment_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $h_id = input('h_id');
        session('h_id', $h_id);
        return $this->fetch('dyn/dyncomment');
    }
    
    /**
     * 设为神评
     * @return \think\response\Json
     */
    public function godyes(){
        $comment_id = input('id');
        $dyn_id = Db::table('up_comment')->where('comment_id', $comment_id)->value('comment_hot_id');
        Db::startTrans();
        try{
            $have = Db::table('up_comment')
                ->where('comment_hot_id',$dyn_id)
                ->where('comment_god',2)
                ->find();
            if($have){
                Db::table('up_comment')
                    ->where('comment_hot_id',$dyn_id)
                    ->where('comment_god',2)
                    ->setField('comment_god',1);
            }
            // 设为神评
            Db::table('up_comment')->where('comment_id', $comment_id)->setField('comment_god',2);
            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 取消神评
     * @return \think\response\Json
     */
    public function godno(){
        $comment_id = input('id');
        Db::startTrans();
        try{
            // 取消神评
            Db::table('up_comment')->where('comment_id', $comment_id)->setField('comment_god',1);
            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 删除评论
     * @return \think\response\Json
     */
    public function commentDel(){
        $comment_id = input('id');
        $dyn_id = Db::table('up_comment')->where('comment_id', $comment_id)->value('comment_hot_id');
        Db::startTrans();
        try{
            // 删除评论
            Db::table('up_comment')->delete($comment_id);
            // 帖子评论数减一
            Db::table('up_hot')->where('h_id', $dyn_id)->setDec('h_message');
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 回复
     * @return mixed|\think\response\Json
     */
    public function commentReply(){
        if(request()->isAjax()){
            $where['comtt_comment_id'] = session('comment_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'u.user_id one_id,u2.user_id reply_id,u.user_username one_name,u2.user_username reply_name';
            $field .= ',m.*';
            $count = Db::table('up_comment_tt')->alias('m')
                ->join('up_u_user u','m.comtt_one_user_id=u.user_id','LEFT')
                ->join('up_u_user u2','m.comtt_reply_user_id=u2.user_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_comment_tt')->alias('m')
                ->join('up_u_user u','m.comtt_one_user_id=u.user_id','LEFT')
                ->join('up_u_user u2','m.comtt_reply_user_id=u2.user_id','LEFT')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('comtt_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $comment_id = input('comment_id');
        session('comment_id', $comment_id);
        return $this->fetch('dyn/commentreply');
    }

    /**
     * 删除回复
     * @return \think\response\Json
     */
    public function comttDel(){
        $comtt_id = input('id');
        $dyn_id = Db::table('up_comment_tt')->where('comtt_id', $comtt_id)->value('comtt_comment_id');
        Db::startTrans();
        try{
            // 删除评论
            Db::table('up_comment_tt')->where('comtt_id', $comtt_id)->delete();
            // 帖子评论数减一
            Db::table('up_comment')->where('comment_id', $dyn_id)->setDec('comment_num');
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}